﻿Imports SIRData
Public Class frmNotaDeIngresoEditar
  Private m_bNuevo As Boolean
  Private m_iNumOrdCom As Int32 = 0
  Private m_iNumNota As Int32 = 0
  Private m_cNumDoc As String = String.Empty

  Public Property bNuevo() As Boolean
    Get
      Return m_bNuevo
    End Get
    Set(ByVal value As Boolean)
      m_bNuevo = value
    End Set
  End Property

  Public Property oiNumOrdCom() As Int32
    Get
      Return m_iNumOrdCom
    End Get
    Set(ByVal value As Int32)
      m_iNumOrdCom = value
    End Set
  End Property

  Public Property ocNumDoc() As String
    Get
      Return m_cNumDoc
    End Get
    Set(ByVal value As String)
      m_cNumDoc = value
    End Set
  End Property

  Public Property oiNumNota() As Int32
    Get
      Return m_iNumNota
    End Get
    Set(ByVal value As Int32)
      m_iNumNota = value
    End Set
  End Property


  Private Sub frmNotaDeIngresoEditar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
      Me.Icon = My.Resources.iconInventory

      If m_bNuevo Then
        Me.Text = ".:Generar Toma de Inventario:."
      Else
        dtpsdFecDoc.Enabled = False
        btnAlmacenOri.Enabled = False
        btnAlmacenDes.Enabled = False
      End If

      Call CargarDetalleOrdenCompra()


    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvDetalle_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvDetalle.RowPrePaint
    Try
      dgvDetalle.Rows(e.RowIndex).DefaultCellStyle.BackColor = IIf(e.RowIndex Mod 2 = 0, Color.Honeydew, Color.White)
      dgvDetalle.Rows(e.RowIndex).DefaultCellStyle.Font = New Font("Lucida Sans", 8)
    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub CargarDetalleOrdenCompra()
    Dim m_OrdenCompraDetalle As New clsOrdenCompraDetalle(g_sConnectString)
    Try
      dgvDetalle.DataSource = m_OrdenCompraDetalle.spOrdenCompraDetalleFiltrar(m_iNumOrdCom)


    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      m_OrdenCompraDetalle = Nothing
      GC.Collect()
    End Try
  End Sub

  Private Sub btnAlmacenOri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlmacenOri.Click
    Dim oform As New frmAlmacenListado
    Try
      oform.ShowDialog()

      If oform.bSelected Then
        lblvNomAlmOri.Tag = oform.osiCodAlm
        lblvNomAlmOri.Text = oform.ovNomAlm
      End If

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnAlmacenDes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlmacenDes.Click
    Dim oform As New frmAlmacenListado
    Try
      oform.ShowDialog()

      If oform.bSelected Then
        lblvNomAlmDes.Tag = oform.osiCodAlm
        lblvNomAlmDes.Text = oform.ovNomAlm
      End If

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnRecibido_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecibido.Click
    Dim oform As New frmEmpleadoListado
    Try
      oform.ShowDialog()

      If oform.bSelected Then
        lblRecibido.Tag = oform.osiCodEmp
        lblRecibido.Text = oform.ovNomEmp
      End If


    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Function ValidarCampos() As Boolean
    Try
      ' Validar Almacen Origen
      If lblvNomAlmOri.Text.Trim.Length = 0 Then
        MessageBox.Show("Por favor debe seleccionar un Almacén Origen.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        btnAlmacenOri.Focus()
        Exit Function
      End If

      'Validar Almacen Destino
      If lblvNomAlmDes.Text.Trim.Length = 0 Then
        MessageBox.Show("Por favor debe seleccionar un Almacén Destino.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        btnAlmacenDes.Focus()
        Exit Function
      End If

      ' Recibido Por 
      If lblRecibido.Text.Trim.Length = 0 Then
        MessageBox.Show("Por favor debe seleccionar un estado para la Toma de Inventario.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        btnRecibido.Focus()
      End If

      Return True

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      GC.Collect()
    End Try
  End Function

  Private Sub GuardarNotaDeIngreso()
    Dim m_NotaDeIngreso As New clsNotaIngresoSalida(g_sConnectString)
    Try
      Me.Cursor = Cursors.WaitCursor

      If ValidarCampos() = False Then Exit Sub

      Me.Enabled = False ' Bloquer el Formulario para guardar los datos

      If m_bNuevo Then
        lbliNumNota.Text = m_NotaDeIngreso.NotaDeIngresoInsertar(m_iNumNota, m_cNumDoc, lblRecibido.Tag, lblvNomAlmOri.Tag, lblvNomAlmDes.Tag, dtpsdFecDoc.Value, m_iNumOrdCom)
        MessageBox.Show("Se ha registrado correctamente la Nota de Ingreso", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      Else
        m_NotaDeIngreso.NotaDeIngresoActualizar(m_iNumNota, lblRecibido.Tag)
        MessageBox.Show("Se ha actualizado correctamente la Nota de Ingreso", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      End If

      gbDatos.Enabled = False
      gbDetalle.Enabled = False
      btnGuardar.Enabled = False

      Me.Enabled = True

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      Me.Cursor = Cursors.Default
      GC.Collect()
    End Try
  End Sub

  Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
    Try
      Call GuardarNotaDeIngreso()


    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnCerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCerrar.Click
    Me.Close()
  End Sub
End Class